// 用户管理模块的接口
import request from '@/utils/request.ts'
import {
  AllRoleResponseData,
  SetRoleData,
  User,
  UserResponseData,
} from '@/api/acl/user/type.ts'

// 枚举地址
enum API {
  // 获取用户列表
  ALL_USER_URL = '/admin/acl/user/',
  // 添加一个新的用户账号
  ADD_USER_URL = '/admin/acl/user/save',
  // 更新用户账号
  UPDATE_USER_URL = '/admin/acl/user/update',
  // 获取全部角色，当前账号拥有的角色接口
  ALL_ROLE_URL = '/admin/acl/user/toAssign/{userId}',
  // 给用户分配角色
  ASSIGN_ROLE_URL = '/admin/acl/user/doAssignRole',
  // 删除用户
  DELETE_USER_URL = '/admin/acl/user/remove/{id}',
  // 批量删除用户
  BATCH_DELETE_USER_URL = '/admin/acl/user/batchRemove',
}

// 获取用户账户信息的接口
export const reqUserInfoList = (
  page: number,
  limit: number,
  username?: string,
) =>
  request.get<any, UserResponseData>(
    API.ALL_USER_URL + `${page}/${limit}?username=${username}`,
  )
// 添加与更新用户账号
export const reqAddOrUpdateUser = (data: User) => {
  if (data.id) {
    return request.put<any, any>(API.UPDATE_USER_URL, data)
  } else {
    return request.post<any, any>(API.ADD_USER_URL, data)
  }
}
// 获取全部职位以及包含当前用户的已有的职位
export const reqAllRole = (userId: number) =>
  request.get<any, AllRoleResponseData>(
    API.ALL_ROLE_URL.replace('{userId}', userId.toString()),
  )

// 分配职位
export const reqSetUserRole = (data: SetRoleData) =>
  request.post<any, any>(API.ASSIGN_ROLE_URL, data)
// 删除用户
export const reqDeleteUser = (id: number) =>
  request.delete<any, any>(API.DELETE_USER_URL.replace('{id}', id.toString()))
// 批量删除用户
export const reqBatchDeleteUser = (ids: (number | undefined)[]) =>
  request.post<any, any>(API.BATCH_DELETE_USER_URL, ids)
